<template>
  <el-dialog title="添加档案" :visible.sync="addDaOpen" width="45%" append-to-body :before-close="closeDelDialog">
    <div v-if="isShowJuan">
      <el-form :model="juanParams" ref="juanForm" :inline="true" v-show="showJuanSearch"
               label-width="68px">
        <el-form-item label="案卷号" prop="keyword">
          <el-input v-model="juanParams.keyword" placeholder="请输入案卷号" clearable style="width: 240px"
                    @keyup.enter.native="handleJuanQuery" maxlength="30" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  @click="handleJuanQuery">查询</el-button>
          <el-button icon="el-icon-refresh"  @click="resetJuanQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd(1)">添加至处理单</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" @click="recovery(1)">恢复</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="juanLoading" :data="juanList" @selection-change="juanSelectionChange" height="300">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" :index="table_index_juan" prop="id" />
        <el-table-column label="案卷题名" prop="title" :show-overflow-tooltip="true" />
        <el-table-column label="案卷号" prop="ajh" :show-overflow-tooltip="true" />
        <el-table-column label="档号" prop="dh" :show-overflow-tooltip="true" />
        <el-table-column label="保管期限">
          <template slot-scope="scope">
              <span v-for="(i, idx) in dict.type.BGQX" :key="idx">
                {{ scope.row.bgqx == i.value ? i.label : '' }}
              </span>
          </template>
        </el-table-column>
        <el-table-column label="归档司局" prop="gdsj" :show-overflow-tooltip="true" />
        <el-table-column label="归档处室" prop="gdcs" :show-overflow-tooltip="true" />
        <el-table-column label="上架时间" prop="sjTime" :show-overflow-tooltip="true" />
        <el-table-column label="位置" prop="position" :show-overflow-tooltip="true" />
      </el-table>
      <pagination v-show="juanTotal > 0" :total="juanTotal" :page.sync="juanParams.pageNum"
                  :limit.sync="juanParams.pageSize" @pagination="getJuanList" />
    </div>


    <div v-else>
      <el-form :model="heParams" ref="heForm"  :inline="true" v-show="showHeSearch"
               label-width="68px">
        <el-form-item label="盒号" prop="keyword">
          <el-input v-model="heParams.keyword" placeholder="请输入盒号" clearable style="width: 240px"
                    @keyup.enter.native="handleHeQuery" maxlength="30" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  @click="handleHeQuery">查询</el-button>
          <el-button icon="el-icon-refresh"  @click="resetHeQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd(0)">添加档案</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-plus" @click="recovery(0)">恢复上架</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="heLoading" :data="heList" @selection-change="heSelectionChange" height="300">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" :index="table_index_he" prop="id" />
        <el-table-column label="盒号" prop="hehao" :show-overflow-tooltip="true" />
        <el-table-column label="档案门类" prop="daml" :show-overflow-tooltip="true" />
        <!--              <el-table-column label="整理方式" prop="zlfs" :show-overflow-tooltip="true" />-->
        <el-table-column label="保管期限">
          <template slot-scope="scope">
              <span v-for="(i, idx) in dict.type.BGQX" :key="idx">
                {{ scope.row.bgqx == i.value ? i.label : '' }}
              </span>
          </template>
        </el-table-column>
        <el-table-column label="起始年度" prop="boxStnd" :show-overflow-tooltip="true" />
        <el-table-column label="终止年度" prop="boxEdnd" :show-overflow-tooltip="true" />
        <el-table-column label="件号(起)" prop="boxStnum" :show-overflow-tooltip="true" />
        <el-table-column label="件号(止)" prop="boxEdnum" :show-overflow-tooltip="true" />
        <el-table-column label="规格" prop="specName" :show-overflow-tooltip="true" />
        <el-table-column label="上架时间" prop="sjTime" :show-overflow-tooltip="true" />
        <el-table-column label="位置" prop="position" :show-overflow-tooltip="true" />
      </el-table>
      <pagination v-show="heTotal > 0" :total="heTotal" :page.sync="heParams.pageNum"
                  :limit.sync="heParams.pageSize" @pagination="getHeList" />
    </div>
  </el-dialog>
</template>

<script>

import {findByConditionToDangAn, findByDa, updateStatus} from "@/api/archive/warehouseManagement/technicalProcessing";

export default {
  name: "addDa",
  props: ['addDaOpen', 'isShowJuan', 'putawayStatus', 'renwuId'],
  archive_Dicts: ['BGQX'],
  data() {
    return {
      juanParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined
      },
      showJuanSearch: true,
      juanLoading: false,
      // 按卷上架表格数据
      juanList: [],
      // 总条数
      juanTotal: 0,
      // 选中数据
      juanIds: [],
      juanids: [],
      // 非单个禁用
      juanSingle: true,
      // 非多个禁用
      juanMultiple: true,


      heParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined
      },
      showHeSearch: true,
      heLoading: false,
      // 按卷上架表格数据
      heList: [],
      // 总条数
      heTotal: 0,
      // 选中数据
      heIds: [],
      // 非单个禁用
      heSingle: true,
      // 非多个禁用
      heMultiple: true,
    }
  },
  watch: {
    isShowJuan: {
      handler(val) {
        if (val) {
          this.getJuanList()
        } else {
          this.getHeList()
        }
      },
      immediate: true
    }
  },
  methods: {
    closeDelDialog() {
      this.$emit('closeDelDialog')
    },
    // 按卷上架 搜索按钮操作
    handleJuanQuery() {
      this.juanParams.pageNum = 1;
      this.getJuanList()
    },
    // 按卷上架 重置按钮操作
    resetJuanQuery() {
      this.resetForm("juanForm");
      this.getJuanList()
    },
    // 按卷上架
    juanSelectionChange(selection) {
      this.juanIds = selection.map(item => item.boxId)
      this.juanSingle = selection.length != 1
      this.juanMultiple = !selection.length
    },
    table_index_juan(index) {
      return (this.juanParams.pageNum - 1) * this.juanParams.pageSize + index + 1;
    },
    getJuanList() {
      this.juanLoading = true
      this.juanParams.sjType = '1'
      this.juanParams.putawayStatus = this.putawayStatus
      findByConditionToDangAn(this.juanParams).then(res=>{
        this.juanLoading = false
        this.juanList = res.rows
        this.juanTotal = res.total
      })
    },

    handleAdd(val) {
      var form = {
        status: this.putawayStatus + '0',
        type: '1',
        id: this.renwuId
      }
      if (val == 1) {
        form.ids = this.juanIds
      } else {
        form.ids = this.heIds
      }
      if (form.ids.length == 0) {
        this.$modal.msgWarning('请选择至少一条数据')
        return
      }
      updateStatus(form).then(res=>{
        this.$modal.msgSuccess('添加成功')
        this.getJuanList()
        this.getHeList()
      })
    },
    recovery(val) {
      var form = {
        status: '1',
        type: '',
        id: this.renwuId
      }
      if (val == 1) {
        form.ids = this.juanIds
      } else {
        form.ids = this.heIds
      }
      if (form.ids.length == 0) {
        this.$modal.msgWarning('请选择至少一条数据')
        return
      }
      updateStatus(form).then(res=>{
        this.$modal.msgSuccess('恢复成功,请重新进行上架')
        this.getJuanList()
        this.getHeList()
      })
    },



    // 按盒上架 搜索按钮操作
    handleHeQuery() {
      this.heParams.pageNum = 1;
      this.getHeList()
    },
    // 按卷上架 重置按钮操作
    resetHeQuery() {
      this.resetForm("heForm");
      this.getHeList()
    },
    // 按盒上架
    heSelectionChange(selection) {
      this.heIds = selection.map(item => item.boxId)
      this.heSingle = selection.length != 1
      this.heMultiple = !selection.length
    },
    table_index_he(index) {
      return (this.heParams.pageNum - 1) * this.heParams.pageSize + index + 1;
    },
    getHeList() {
      this.heLoading = true
      this.heParams.sjType = '0'
      this.heParams.putawayStatus = this.putawayStatus
      findByConditionToDangAn(this.heParams).then(res=>{
        this.heLoading = false
        this.heList = res.rows
        this.heTotal = res.total
      })
    }
  }
}
</script>

<style scoped>

</style>
